home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / science / piw131.zip / PIW.CPP < prev    next >
Text File  |  1994-07-29  |  5KB  |  145 lines

  1. // ------- PiW.Cpp Compute Pi to a million or so decimal places in Windows
  2.  
  3. char *Name   = "PiW - Compute Pi to a million or so decimal places in Windows";
  4. char *Version= "Version 1.31, last revised: 1994-07-29, 0600 hours";
  5. char *Author = "Copyright (c) 1981-1994 by author: Harry J. Smith,";
  6. char *Address= "19628 Via Monte Dr., Saratoga, CA 95070.  All rights reserved.";
  7.  
  8. #include "MultiFDW.h"
  9.  
  10. // Developed in Borland C++ 3.1 and 4.0 for Windows.
  11.  
  12.   char FileSt[13]; // Disk output file name
  13.   char LogSt[13];  // Log output file name
  14.   long Places;     // number of decimal places to compute Pi
  15.   Bool WReset;     // if WReset then Write reset data after each iteration of pi
  16.   Bool ReStart;    // if ReStart then Read reset data before computation of pi
  17.   Bool UseAB;      // if UseAB then use algorithm b else use algorithm a
  18.   Bool DOnly;      // True if only raw digits of a number are to be output
  19.   int  DpG;        // Digits per group in number output display >= 1 or 0
  20.  
  21. void init() {
  22.   clrscr();
  23.   cout << Name <<nL<< Version <<nL<< Author <<nL<< Address << dL;
  24.   strcpy( FileSt, "PiW.Out");
  25.   strcpy( LogSt,  "PiW.Log");
  26.   cout << "Will write file: " << FileSt
  27.        << " containing Pi." << dL;
  28.   cout << "Will write file: " << LogSt
  29.        << " containing a log of operations." << dL;
  30.   cout << "Will write file: PiReset.Txt"
  31.        << " containing a ReStart file." << dL;
  32.   cout << "Will write file: PiReset.Old"
  33.        << " containing a ReStart file backup." << dL;
  34.   cout << "How many decimal places do you need? (n < 0 to restart) (0 to quit): ";
  35.   cin >> Places;
  36.   if (Places == 0)  exit(0);
  37.   cout << "Use algorithm b? (Y/N): ";
  38.   char YesNo;  cin >> YesNo;
  39.   UseAB = (YesNo == 'Y') || (YesNo == 'y');
  40.   ReStart = (Places < 0);
  41.   Places = labs(Places);
  42.   InitMultiF(); // Initialize Multi-precision packages F and SI
  43.  
  44.   cout <<
  45.    "How many digits between commas in output? (0 for no , < 0 for digits only): ";
  46.   cin >> DpG;
  47.   DOnly = (DpG < 0);
  48.   if (DpG <= -32768L)  DpG = 0;
  49.   DpG = abs( DpG);
  50.  
  51.   Echo = True;
  52.   Disk.open( FileSt, ios::trunc); // Open output file for rewrite
  53.   if (Disk.fail())  { cout << "Cannot open " << FileSt << nL;  exit(1); }
  54.   if (!ReStart)
  55.     LogF.open( LogSt, ios::trunc);  // Open Log file for rewrite
  56.   else
  57.     LogF.open( LogSt, ios::app);    // Open Log file for append
  58.   if (LogF.fail())  { cout << "Cannot open " << LogSt << nL;  exit(1); }
  59.   cout << nL;  LogF << nL;  Disk << nL;
  60.   Disk.close();
  61.   GetDateTime();
  62.   cout << "Start of PiW.Cpp ";
  63.   OutDateTime( cout);
  64.   if (Echo) {
  65.     if (!ReStart)
  66.       LogF << Name <<nL<< Version <<nL<< Author <<nL<< Address << dL;
  67.     else
  68.       LogF << "Re";
  69.     LogF << "Start of PiW.Cpp ";
  70.     OutDateTime( LogF);
  71.   }
  72. //  int NRegs = 5;  MuSetMMax( NRegs);
  73. } // --end-- init
  74.  
  75. // ------- Terminate program
  76. void quit()
  77. {
  78.   GetDateTime();
  79.   cout << "End of PiW.Cpp ";
  80.   OutDateTime( cout);
  81.   if (Echo) {
  82.     LogF << "End of PiW.Cpp ";
  83.     OutDateTime( LogF);
  84.   }
  85.   Disk.close();  LogF.close();
  86. //HeapOk(); // Heap check not supported by MS Windows
  87.   exit(0);
  88. } // --end-- quit
  89.  
  90. int main( void) // ------- PiW.Cpp
  91. {
  92.   long Tot;
  93.   char Ch;
  94.  
  95.   init();
  96.  
  97.   DiagOn = True;  TV3 = TV2 = TV1 = TV0 = DosClock();
  98.   FTn = 3;
  99.   FMC = 5 + (Places + 20) / MuDMax;
  100.   cout << "Trying to compute Pi to about " << (FMC - 4) * MuDMax
  101.        << " decimal places." << nL;
  102.   LogF << "Trying to compute Pi to about " << (FMC - 4) * MuDMax
  103.        << " decimal places." << nL;
  104.   MultiF X( FMC);
  105.   WReset = True;
  106.  
  107.   if (UseAB)
  108.     PiAB( X, WReset, ReStart); // Compute X = Pi using algorithm b
  109.   else
  110.     PiAA( X, WReset, ReStart); // Compute X = Pi using algorithm a
  111.  
  112.   if (!MuAbort) {
  113.     do {
  114.       Disk.open( FileSt, ios::trunc); // Open output file for rewrite
  115.       if (Disk.fail()) {
  116.         cout << "Cannot open " << FileSt
  117.              << ", Type a character, Enter to retry" << nL;
  118.          cin >> Ch;
  119.       }
  120.     } while (Disk.fail());
  121.  
  122.     MuDOnly = DOnly;
  123.     MuDpG = DpG;
  124.     Diag("Writing Pi to disk");
  125.     cout << nL;
  126.     LogF << nL;
  127.     if (!DOnly) {
  128.       cout       << "Pi = m.n E+0, m.n =" << dL;
  129.       LogF       << "Pi = m.n E+0, m.n =" << dL;
  130.       Disk << nL << "Pi = m.n E+0, m.n =" << dL;
  131.     }
  132.     Tot = 0;
  133.     X.WritLn( Disk, Tot);
  134.     Tot = 0;
  135.     FDn = 700;
  136.     X.WritLn( LogF, Tot);  LogF << nL;
  137.     X.WritLn( cout, Tot);  cout << nL;
  138.     Diag("All done");
  139.   }
  140.   quit();
  141.   return 0;
  142. }
  143.  
  144. // --end-- PiW.Cpp Compute Pi to a million or so decimal places in Window
  145.